<?php

if(!defined('IN_PROJECTOR')) exit('Access Denied');

class CSVFile{
	private $data = array();
	private $item = array();
	
	public function setItem($item){
		if(!is_array($item)){
			$this->item = explode(',', $item);
		}else{
			$this->item = $item;
		}
	}
	
	public function importArray($data){
		$this->data = $data;
	}
	
	public function exportArray(){
		return $this->data;
	}
	
	public function importCsv($file, $skipFirstLine = true){
		$lines = explode("\n", $file);
		if($skipFirstLine){
			unset($lines[0]);
		}
		
		foreach($lines as $line){
			$node = explode(',', $line);
			$array = array();
			$i = 0;
			foreach($this->item as $item){
				$array[$item] = $node[$i];
				$i++;
			}
			$this->data[] = $array;
		}
	}
	
	public function exportCsv(){
		if(count($this->data) <= 0){
			return '';
		}
	
		$file = implode(',', $this->item);
		foreach($this->data as $v){
			$file.= "\r\n".implode(',', $v);
		}
		return $file;
	}
	
	public function clearData(){
		$this->data = array();
	}
	
	public function clearItem(){
		$this->item = array();
	}
}

?>